package com.jiangnan.controller;

import com.jiangnan.anno.Log;
import com.jiangnan.pojo.PageResult;
import com.jiangnan.pojo.Result;
import com.jiangnan.pojo.Student;
import com.jiangnan.pojo.StudentQueryParam;
import com.jiangnan.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam studentQueryParam) {
        log.info("page:{}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    @Log
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Integer> ids) {
        log.info("deleteById:{}", ids);
        studentService.deleteByIds(ids);
        return Result.success();
    }

    @Log
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("save:{}", student);
        studentService.save(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("getById:{}", id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("update:{}", student);
        studentService.update(student);
        return Result.success();
    }

    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("updateViolation:{}", id);
        studentService.updateViolation(id, score);
        return Result.success();
    }


}
